import requests
import time


class Api:
    host:str
    device_no:str

    def __init__(self,host,device_no):
        self.host = host
        self.device_no = device_no

    def start_app(self,package_name:str,activity_name:str,note:str=''):
        """
            打开应用指令

            package_name:    打开应用的包名

            activity_name:  应用的服务名

            note:   悬浮框提示文本
        """
        url = f'{self.host}/cmd/startApp/{self.device_no}?packageName={package_name}&activityName={activity_name}&note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])
    
    def swipe(self,start_x:int,start_y:int,end_x:int,end_y:int,duration:int,note:str):
        """
            滑动指令

            start_x: 开始横坐标

            start_y: 开始纵坐标

            end_x:   结束横坐标

            end_y:   结束纵坐标

            duration:   滑动时长,单位:毫秒

            note:   悬浮框提示文本
        """
        url = f'{self.host}/cmd/swipe/{self.device_no}?startX={start_x}&startY={start_y}&endX={end_x}&endY={end_y}&duration={duration}&note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])
    
    def input(self,text:str,note:str=''):
        """
            输入文本,必须焦点在输入框中

            text:文本内容

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/input/{self.device_no}?text={text}&note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])
    
    def clear_text(self,note:str=''):
        """
            清空输入框

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/clearText/{self.device_no}?note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])

    def input_key(self,keycode:str|int,note:str=''):
        """
            进行按键

            keycode:    按键的编码,(back键=1;home键=2)

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/inputKey/{self.device_no}?keycode={keycode}&note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])
    
    def goback(self,note:str=''):
        """
            进行返回按键

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/goBack/{self.device_no}?note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])
    
    def gohome(self,note:str=''):
        """
            进行主页按键

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/goHome/{self.device_no}?note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])
    
    def click(self,x:int,y:int,note:str='')-> bool:
        """
            进行点击

            x:横坐标

            y:纵坐标

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/click/{self.device_no}?x={x}&y={y}&note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])
    
    def wm_size(self,note:str=''):
        """
            获取手机屏幕大小

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/wmSize/{self.device_no}?note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return tuple(response.json()['data'])
    
    def screen_capture(self,note:str='')->bytes:
        """
            进行手机截屏(tips:需要先在手机上进行截屏权限设置)

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/screenCapture/{self.device_no}?note={note}'
        response = requests.post(url)
        response.raise_for_status()
        return response.content()
    
    def get_clipboard_ontent(self,note:str=''):
        """
            获取手机粘贴版内容

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/getClipboardContent/{self.device_no}?&note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return str(response.json()['data'])
    
    def set_clipboard_content(self,text:str,note:str='')-> bool:
        """
            设置手机粘贴版内容
            
            text:文本

            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/setClipboardContent/{self.device_no}?text={text}&note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return bool(response.json()['data'])
    
    def get_uixml(self,note:str=''):
        """
            获取UI的xml内容
            
            note:悬浮框提示文本
        """
        url = f'{self.host}/cmd/getUiXml/{self.device_no}?note={note}'
        response = requests.post(url)
        print(response.text)
        response.raise_for_status()
        return str(response.json()['data'])
    
if __name__ == "__main__":
    # 初始化
    api = Api('http://121.37.213.206:8881','931292')
    
    # 获取手机屏幕大小
    # print(api.wm_size())
    
    # 打开抖音APP
    packageName='com.ss.android.ugc.aweme'
    activityName='com.ss.android.ugc.aweme.main.MainActivity'
    note='打开抖音'
    # result = api.start_app(packageName,activityName,note)

    # 休眠2妙
    # time.sleep(2)
    
    # 滑动
    note = '滑动'
    # api.swipe(500,1500,500,500,150,note)

    # 输入文本
    # api.input('自动助手')

    # time.sleep(2)

    # api.clear_text()

    # 按键,返回键=1\
    # note = '模拟按键(返回键)'
    # api.input_key(1,'')

    # time.sleep(2)

    # 模拟点击
    note = '点击'
    # api.click(500,500,note)

    print(api.get_uixml())